<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\admin;

use App\Controller\admin\common\Util;
use App\Exception\BusinessException;
use App\Model\Admin\Option;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\View\RenderInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 系统设置.
 */
#[AutoController]
class ConfigController extends Base
{
    /**
     * 不需要验证权限的方法.
     * @var string[]
     */
    protected array $noNeedAuth = ['get'];

    /**
     * 账户设置.
     */
    public function index(RenderInterface $render): ResponseInterface
    {
        return $render->render('config/index');
    }

    /**
     * 获取配置.
     */
    public function get()
    {
        return $this->getByDefault();
    }

    /**
     * 更改.
     */
    public function update(): ResponseInterface
    {
        $request = $this->request;
        $post = $request->all();
        $config = $this->getByDefault();
        $data = [];
        foreach ($post as $section => $items) {
            if (! isset($config[$section])) {
                continue;
            }
            switch ($section) {
                case 'logo':
                    $data[$section]['title'] = htmlspecialchars($items['title'] ?? '');
                    $data[$section]['image'] = Util::filterUrlPath($items['image'] ?? '');
                    $data[$section]['icp'] = htmlspecialchars($items['icp'] ?? '');
                    $data[$section]['beian'] = htmlspecialchars($items['beian'] ?? '');
                    $data[$section]['footer_txt'] = htmlspecialchars($items['footer_txt'] ?? '');
                    break;
                case 'menu':
                    $data[$section]['data'] = Util::filterUrlPath($items['data'] ?? '');
                    $data[$section]['accordion'] = ! empty($items['accordion']);
                    $data[$section]['collapse'] = ! empty($items['collapse']);
                    $data[$section]['control'] = ! empty($items['control']);
                    $data[$section]['controlWidth'] = (int) $items['controlWidth'] ?? 500;
                    $data[$section]['select'] = (int) $items['select'] ?? 0;
                    $data[$section]['async'] = true;
                    break;
                case 'tab':
                    $data[$section]['enable'] = true;
                    $data[$section]['keepState'] = ! empty($items['keepState']);
                    $data[$section]['preload'] = ! empty($items['preload']);
                    $data[$section]['session'] = ! empty($items['session']);
                    $data[$section]['max'] = Util::filterNum($items['max'] ?? '30');
                    $data[$section]['index']['id'] = Util::filterNum($items['index']['id'] ?? '0');
                    $data[$section]['index']['href'] = Util::filterUrlPath($items['index']['href'] ?? '');
                    $data[$section]['index']['title'] = htmlspecialchars($items['index']['title'] ?? '首页');
                    break;
                case 'theme':
                    $data[$section]['defaultColor'] = Util::filterNum($items['defaultColor'] ?? '2');
                    $data[$section]['defaultMenu'] = $items['defaultMenu'] ?? '' == 'dark-theme' ? 'dark-theme' : 'light-theme';
                    $data[$section]['defaultHeader'] = $items['defaultHeader'] ?? '' == 'dark-theme' ? 'dark-theme' : 'light-theme';
                    $data[$section]['allowCustom'] = ! empty($items['allowCustom']);
                    $data[$section]['banner'] = ! empty($items['banner']);
                    break;
                case 'colors':
                    foreach ($config['colors'] as $index => $item) {
                        if (! isset($items[$index])) {
                            $config['colors'][$index] = $item;
                            continue;
                        }
                        $data_item = $items[$index];
                        $data[$section][$index]['id'] = $index + 1;
                        $data[$section][$index]['color'] = $this->filterColor($data_item['color'] ?? '');
                        $data[$section][$index]['second'] = $this->filterColor($data_item['second'] ?? '');
                    }
                    break;
            }
        }
        $config = array_merge($config, $data);
        $name = 'system_config';
        Option::where('name', $name)->update([
            'value' => json_encode($config),
        ]);
        return $this->json(0);
    }

    /**
     * 基于配置文件获取默认权限.
     */
    protected function getByDefault(): mixed
    {
        $name = 'system_config';
        $config = Option::where('name', $name)->value('value');
        if (empty($config)) {
            $config = file_get_contents(BASE_PATH . '/public/config/pear.config.json');
            if ($config) {
                $option = new Option();
                $option->name = $name;
                $option->value = $config;
                $option->save();
            }
        }
        return json_decode($config, true);
    }

    /**
     * 颜色检查.
     * @throws BusinessException
     */
    protected function filterColor(string $color): string
    {
        if (! preg_match('/\#[a-zA-Z]6/', $color)) {
            throw new BusinessException(500, '参数错误');
        }
        return $color;
    }
}
